/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ControlEscolar.AdminStudent;

import com.ControlEscolar.Beans.Student;
import com.ControlEscolar.Exceptions.ServiceExp;
import com.ControlEscolar.Services.StudentService;
import java.sql.Date;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

/**
 *
 * @author jalbs
 */
public class AdminStudent extends MappingDispatchAction{
    private static final String MODIFY="m";
    private static final String INFORMATION="i";
    ArrayList results=new ArrayList();
    StudentService service = new StudentService();

    public ActionForward viewAll (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
          results=service.getStudentList();
          HttpSession session = request.getSession();
          session.setAttribute("studentList", results);
          return mapping.findForward("success");
    }

    public void modifyStudent (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
          
          StudentForm studentForm = (StudentForm)form;
          Student student=formToBean(studentForm);
          studentForm.setAction("");
          try{
          service.updateStudent(student);
          response.getWriter().print("success");
          }catch(ServiceExp ex){
            response.getWriter().print("No se puede modificar este alumno, Verifique sus datos");
          }

    }

    public ActionForward getStudentInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
          String studentKey=request.getParameter("id");
          Student student = new Student();
          student = service.getStudent(studentKey);
          StudentForm studentForm = (StudentForm)form;
          beanToForm(studentForm, student);
          studentForm.setAction(INFORMATION);
          return mapping.findForward("success");

    }

    public ActionForward getStudentToModify(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
          String studentKey=request.getParameter("id");
          Student student = new Student();
          student = service.getStudent(studentKey);
          StudentForm studentForm = (StudentForm)form;
          beanToForm(studentForm, student);
          studentForm.setAction(MODIFY);
          return mapping.findForward("success");

    }

    public void addStudent(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{

          StudentForm studentForm = (StudentForm)form;
          Student student = formToBean(studentForm);
          try {
             service.createStudent(student);
             response.getWriter().print("success");
          }catch(Exception ex){
             response.getWriter().print("Ya existe este alumno o algun dato es incorrecto");
          }

    }

    private Student formToBean(StudentForm studentForm) {
        Student student = new Student();
        student.setAddress(studentForm.getAddress());
        student.setBirthCert(studentForm.getBirthCert());
        student.setBirthday(stringToDate(studentForm.getBirthday()));
        student.setBloodType(studentForm.getBloodType());
        student.setCURP(studentForm.getCURP());
        student.setCel(studentForm.getCel());
        student.setDICT_REV(studentForm.getDICT_REV());
        student.setEmail(studentForm.getEmail());
        student.setFirstLastName(studentForm.getFirstLastName());        
        student.setIllness(studentForm.getIllness());
        student.setMartialStatus(studentForm.getMartialStatus());
        student.setName(studentForm.getName());
        student.setNationality(studentForm.getNationality());
        student.setPhone(studentForm.getPhone());
        student.setCert(studentForm.getCert());
        student.setSecondLastName(studentForm.getSecondLastName());
        student.setSex(studentForm.getSex());
        student.setStudentKey(studentForm.getStudentKey());
        student.setState(studentForm.getState());
        return student;
    }

    private void beanToForm(StudentForm studentForm, Student student) {
        studentForm.setAddress(student.getAddress());
        studentForm.setBirthCert(student.getBirthCert());
        studentForm.setBirthday(dateToString(student.getBirthday()));
        studentForm.setBloodType(student.getBloodType());
        studentForm.setCURP(student.getCURP());
        studentForm.setCel(student.getCel());
        studentForm.setDICT_REV(student.getDICT_REV());
        studentForm.setEmail(student.getEmail());
        studentForm.setFirstLastName(student.getFirstLastName());
        studentForm.setIllness(student.getIllness());
        studentForm.setMartialStatus(student.getMartialStatus());
        studentForm.setName(student.getName());
        studentForm.setNationality(student.getNationality());
        studentForm.setPhone(student.getPhone());
        studentForm.setCert(student.getCert());
        studentForm.setSecondLastName(student.getSecondLastName());
        studentForm.setSex(student.getSex());
        studentForm.setStudentKey(student.getStudentKey());
        studentForm.setState(student.getState());
    }

    private Date stringToDate(String yearMonthDay){        
        if(yearMonthDay!=null){
            String[] splittedDate=yearMonthDay.split("/");
            if(splittedDate.length==3){
                Date date=Date.valueOf(""+splittedDate[0]+"-"+splittedDate[1]+"-"+splittedDate[2]);
                return date;
            }
            splittedDate=yearMonthDay.split("-");
            if(splittedDate.length==3){
                Date date=Date.valueOf(""+splittedDate[0]+"-"+splittedDate[1]+"-"+splittedDate[2]);
                return date;
            }
        }
        return null;
    }

    private String dateToString(Date date){
        String[] ymd=date.toString().split("-");
        String s=""+(ymd[0]+"-"+ymd[1]+"-"+ymd[2]);
        return s;
    }

}
